package com.taru.service.impl;

import com.taru.dao.CommentMapper;
import com.taru.dao.DiscussionMapper;
import com.taru.dao.PhotoMapper;
import com.taru.model.Discussion;
import com.taru.model.Photo;
import com.taru.service.DiscussionService;
import com.taru.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class DiscussionServiceImpl implements DiscussionService {

    @Autowired
    DiscussionMapper discussionMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    PhotoMapper photoMapper;
    @Override
    public int insertDisscussion(Discussion discussion, Photo photo) {
        int k =0;
        String ID = Constant.UUID;
        discussion.setDiscussionId(ID);
        photo.setArticleId(ID);
        int i = photoMapper.insertPhoto(photo);
       int j = discussionMapper.insertDisscussion(discussion);
       if(i == 1 && j == 1){
           k = 1;
       }
        return k;
    }

    @Override
    public int updateDisscission(Discussion discussion, Photo photo) {
        int k =0;
       int i = discussionMapper.updateDisscission(discussion);
       int j = photoMapper.updatePhotoById(photo);
        if(i == 1 && j == 1){
            k = 1;
        }
        return k;
    }

    @Override
    public int deleteDissionById(String discussionId) {
        return discussionMapper.deleteDissionById(discussionId);
    }

    @Override
    public Discussion queryDissonById(String discussionId) {
       Discussion discussion = discussionMapper.queryDissonById(discussionId);
        discussion.setCommentList(commentMapper.queryCommentById(discussionId));
        discussion.setPhoto(photoMapper.queryPhotoById(discussionId));
        return discussion;
    }

    @Override
    public List<Discussion> queryDission() {
        List<Discussion> discussionList = discussionMapper.queryDission();
        for (Discussion list: discussionList) {
            list.setPhoto(photoMapper.queryPhotoById(list.getDiscussionId()));
        }
        return discussionList;
    }
}
